<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Walter Tamboer
 * @package Framework03
 */

/**
 * The error handler plugin.
 */
class FW_Controller_Plugin_ErrorHandler extends FW_Controller_Plugin_Abstract
{
    /**
     * @var string Controller to use for errors; defaults to 'error'
     */
    private $errorController = 'error';

    /**
     * @var string Action to use for errors; defaults to 'error'
     */
    private $errorAction = 'error';

    /**
     * @var boolean Whether or not we've been here before.
     */
    private $firstTimer = true;

    /**
     * Called after a request has been dispatched.
     * @param FW_Controller_Request_Abstract $request
     * @param FW_Controller_Response_Abstract $response
     */
    public function postDispatch(FW_Controller_Request_Abstract $request, FW_Controller_Response_Abstract $response)
    {
        if ($this->firstTimer)
        {
            $this->firstTimer = false;

            if ($response->hasException())
            {
                $request->setControllerName($this->errorController)->setActionName($this->errorAction)->setDispatched(false);
            }
        }
    }
}
